package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyPlanAgreementAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.FinancingArgeementEntity;
import com.kkd.kkdapp.http.entity.FinancingArgeementEntityItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class MyPlanAgreementActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private Context mContext = this;
	private final static int CODE_ZERO = 1;
	private final static int CODE_ELSE = 2;
	private int pageNO = 1;
	private int pageSize = 20;
	private ListView mListview;
	private ImageView iv_null;
	private TextView tv_null;
	private MyPlanAgreementAdapter mAdapter;
	private List<FinancingArgeementEntityItem> dataList = new ArrayList<FinancingArgeementEntityItem>();
	private String transferId;
	private boolean REFRESHFLAG = true;
	private boolean isShowToastOnRefresh = false;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_ZERO:
				FinancingArgeementEntity data = (FinancingArgeementEntity) msg.obj;
				if (data.getPageNO() > data.getPageCount()) {
					mXListView.stopLoadMore(-1);
					if (REFRESHFLAG) {
						mXListView.stopLoadMore(1);
					}
				} else {
					mXListView.setNoMore(false);
					if (pageNO == 1) {
						dataList.clear();
					}
					if (data.getContracts() == null
							|| data.getContracts().size() <= 0) {
						return;
					}
					dataList.addAll(data.getContracts());
					mAdapter.notifyDataSetChanged();
					if (data.getContracts().size() < pageSize) {
						mXListView.stopLoadMore(1);
					} else {
						mXListView.stopLoadMore(0);
					}
				}
				mXListView.stopRefresh();
				mXListView.initState();
				if (REFRESHFLAG) {
					if (dataList == null || dataList.size() == 0) {
						iv_null.setVisibility(View.VISIBLE);
						tv_null.setVisibility(View.VISIBLE);
						tv_null.setText("暂无~");
						iv_null.setImageResource(R.drawable.img_null);
					} else {
						tv_null.setVisibility(View.GONE);
						iv_null.setVisibility(View.GONE);
					}
				}

				if (!isShowToastOnRefresh) {
					isShowToastOnRefresh = true;
				}
				break;

			case CODE_ELSE:
				mXListView.stopLoadMore(1);
				mXListView.stopRefresh();
				if (dataList == null || dataList.size() == 0) {
					if (msg.arg1 == KkdHttpClient.NO_NET) {
						iv_null.setImageResource(R.drawable.img_null_net);
						tv_null.setText("网络错误，请稍候下拉刷新重试");
					} else {
						iv_null.setImageResource(R.drawable.img_null_net);
						tv_null.setText("服务异常，请稍候下拉刷新重试");
					}
					iv_null.setVisibility(View.VISIBLE);
					tv_null.setVisibility(View.VISIBLE);
				}
				String desc = (String) msg.obj;
				if (isShowToastOnRefresh) {
					if (!TextUtils.isEmpty(desc)) {
						AppUtil.makeToastForCustomer(mContext, desc);
					}
				} else {
					isShowToastOnRefresh = true;
				}
				break;

			default:
				break;
			}
		}
	};
	private KkdXlistView mXListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_plan_agreement);
		transferId = getIntent().getStringExtra("transferId");
		initView();
		initListener();
		requestFinancingArgeement();
	}

	private void initView() {
		mXListView = (KkdXlistView) findViewById(R.id.xlv);
		mXListView.setKKDScrollListener(this);
		mListview = mXListView.mListView;
		mListview.setDividerHeight(0);
		iv_null = (ImageView) findViewById(R.id.id_no_content);
		tv_null = (TextView) findViewById(R.id.id_no_content_text);
		mAdapter = new MyPlanAgreementAdapter(mContext, dataList);
		mListview.setAdapter(mAdapter);
		mXListView.startRefresh();
	}

	private void initListener() {
		findViewById(R.id.condition_img).setOnClickListener(this);
	}

	/**
	 * 请求协议，合同接口
	 */
	private void requestFinancingArgeement() {
		KkdHttpMagager.getInstance().getDebtArgeementManager(transferId,
				pageNO, pageSize, FinancingArgeementEntity.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						FinancingArgeementEntity data = (FinancingArgeementEntity) responseBody;
						Message msg = Message.obtain();
						msg.what = CODE_ZERO;
						msg.obj = data;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.what = CODE_ELSE;
						msg.obj = desc;
						msg.arg1 = statusCode;
						mHandler.sendMessage(msg);
					}
				});
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.condition_img:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		REFRESHFLAG = true;
		requestFinancingArgeement();
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		REFRESHFLAG = false;
		requestFinancingArgeement();
	}
}
